#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTimer>
#include <QDebug>
#include <QtWidgets>
#include "toast.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    isShow = false;

    // 设置按钮样式跟随侧边栏状态而变化
    setBtnStyle(false);

    //直接移动侧边栏无效，可能是界面重绘不支持。所以间隔0.2秒后再移动
    QTimer::singleShot(200, this, SLOT(slotMoveWidget()));

    //初始化动画
    anim1 = new QPropertyAnimation(ui->widSide, "geometry");
    anim1->setEasingCurve(QEasingCurve::Linear);
    anim1->setDuration(2000);
    anim2 = new QPropertyAnimation(ui->btnSide, "geometry");
    anim2->setEasingCurve(QEasingCurve::Linear);
    anim2->setDuration(2000);
}

MainWindow::~MainWindow()
{
    delete ui;
}

/* 设置按钮样式跟随侧边栏状态而变化 */
void MainWindow::setBtnStyle(bool isShowSide)
{
    QString qss;
    if(isShowSide) {
        qss = "QPushButton {background-image: url(:/image/ic_left_normal.png);border-radius: 6px;}"
              "QPushButton:hover {background-image: url(:/image/ic_left_normal.png);}"
              "QPushButton:pressed {background-image: url(:/image/ic_left_pressed.png);}";
    } else {
        qss = "QPushButton {background-image: url(:/image/ic_right_normal.png);border-radius: 6px;}"
              "QPushButton:hover {background-image: url(:/image/ic_right_normal.png);}"
              "QPushButton:pressed {background-image: url(:/image/ic_right_pressed.png);}";
    }
    ui->btnSide->setStyleSheet(qss);
}


void MainWindow::on_btnSide_clicked()
{
    // 侧边栏和按钮动画
    if(isShow) {
        isShow = false;
        anim1->setStartValue(QRect(0, 0, ui->widSide->width(), ui->widSide->height()));
        anim1->setEndValue(QRect(-ui->widSide->width(), 0, ui->widSide->width(), ui->widSide->height()));

        anim2->setStartValue(QRect(ui->widSide->width(), 0, ui->widSide->width(), ui->widSide->height()));
        anim2->setEndValue(QRect(0, 0, ui->widSide->width(), ui->widSide->height()));
    } else {
        isShow = true;
        anim1->setStartValue(QRect(-ui->widSide->width(), 0, ui->widSide->width(), ui->widSide->height()));
        anim1->setEndValue(QRect(0, 0, ui->widSide->width(), ui->widSide->height()));

        anim2->setStartValue(QRect(0, 0, ui->widSide->width(), ui->widSide->height()));
        anim2->setEndValue(QRect(ui->widSide->width(), 0, ui->widSide->width(), ui->widSide->height()));
    }
    anim1->start();
    anim2->start();

    setBtnStyle(isShow);
}

void MainWindow::slotMoveWidget()
{
    ui->widSide->move(-ui->widSide->width(), 0);
}

void MainWindow::on_pushButton_5_clicked()
{
//    QRect desktopRect = QApplication::desktop()->availableGeometry();
//    qDebug() << "width = " << this->width() << "; height = "<< this->height();
//    qDebug() << "w = " << desktopRect.width() << "; h = "<< desktopRect.height();
    Toast *toast = new Toast(tr("这是toast提示信息这是toast提示信息"),this, true);
    toast->setGeometry(this->width() / 2 - toast->getLabelWidth() / 2, this->height() - 60, toast->width(), toast->height());
    toast->show();
}
